<?php

class Form_SignUp extends App_Dojo_Form
{
    private $publickey = '6LcogwoAAAAAAOJm8FmbMZWXBVc0O_5LgAO4bs4h';
	private $privatekey = '6LcogwoAAAAAADf7guDDvM_ImAcjq8zqeMc3f52O';
	
	public $_noDecorators = array(
                                    'DijitElement',
	);
	
	public $_selectUserTypes = array();
		    
    public function init()
    {
		$this->setName('formSignUp');
 
        $this->setMethod('post');
        	
		$this->setDecorators(array(    
			'FormElements',    
			array(
				'HtmlTag', 
				array(
					'tag' => 'dl',
					'class' => 'form normal'
					)
			),    
			'DijitForm',
		));
        
        $this->addElement(
        	'ValidationTextBox', 
        	'first_name', 
        	array(
	            'label'          	=> 'First name',
	            'required'       	=> true,
        		'promptMessage'		=> 'Enter your first name.',
	            'invalidMessage' 	=> 'Please type your first name.',
	            'trim'      		=> true,
	            'maxlength'      	=> 50,
        		'validators' 		=> array(
					'NotEmpty', 
        			array('StringLength', true, array(2, 50))
            	),
        	)
        );
        
        $this->addElement(
        	'ValidationTextBox', 
        	'last_name', 
        	array(
	            'label'          	=> 'Last name',
	            'required'       	=> true,
        		'promptMessage'		=> 'Enter your last name.',
	            'invalidMessage' 	=> 'Please type your last name.',
	            'trim'      		=> true,
	            'maxlength'      	=> 50,
        		'validators' 		=> array(
					'NotEmpty', 
        			array('StringLength', true, array(2, 50))
            	),
        	)
        );
        
        $this->addElement(
        	'ValidationTextBox', 
        	'username', 
        	array(
	            'label'          	=> 'Username',
	            'required'       	=> true,
        		'promptMessage'		=> 'Enter your username.',
	            'invalidMessage' 	=> 'Please type your username.',
	            'trim'      		=> true,
	            'maxlength'      	=> 50,
        		'validators' 		=> array(
					'NotEmpty', 
        			array('StringLength', true, array(2, 50)),
        			new App_Validate_UniqueUsername
            	),
        	)
        );
		
        $this->addElement ( 
			'ValidationTextBox', 
			'email', 
			array (
				'required' 			=> true, 
				'label' 			=> 'Email address', 
				'regExp' 			=> '\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b',
				'promptMessage'		=> 'Enter your Email address.', 
				'invalidMessage' 	=> 'Please provide a valid Email address.', 
				'filters' 			=> array ('StringTrim', 'StringToLower' ), 
				'maxlength'      	=> 200,
				'validators' 		=> array (
					'NotEmpty', 
					array ('StringLength', true, array (6, 200 )), 
					array ('Regex', true, array ('/\w+/i'))
				) 
			)
		);
		
		$this->addElement(
        	'PasswordTextBox', 
        	'password', 
        	array(
	            'label'          	=> 'Password',
	            'required'       	=> true,
	            'trim'       	 	=> true,
				'invalidMessage' 	=> 'Invalid password; ' . 'must be at least 6 alphanumeric characters',			
				'regExp'         	=> '^[a-z0-9]{6,}$'        
			)
		);
  
        $this->addElement(
        	'PasswordTextBox', 
        	'confirmPassword', 
        	array(
	            'label'          	=> 'Confirm password',
	            'required'       	=> true,
	            'trim'       	 	=> true,
				'invalidMessage' 	=> 'Please re-enter your password',
        		'onBlur'		 	=> 'validatePassword()',			
				'regExp'         	=> '^[a-z0-9]{6,}$',        
	            'validators' 		=> array(
					'NotEmpty', 
            	),
			)
		);
		
		$this->addElement(
        	'ValidationTextBox', 
        	'phone_number', 
        	array(
	            'label'          	=> 'Phone number',
	            'required'       	=> false,
	            'invalidMessage' 	=> 'Please type a valid phone number.',
	            'trim'      		=> true,
	            'maxlength'      	=> 20,
        		'validators' 		=> array(
					'NotEmpty', 
        			array('StringLength', true, array(2, 50))
            	),
        	)
        );
        
        $options = array('theme' => 'white', 'lang' => 'en');
		$recaptcha = new Zend_Service_ReCaptcha($this->publickey, $this->privatekey, null, $options);
        $captcha = new Zend_Form_Element_Captcha('challenge',
              array('captcha'        => 'ReCaptcha',
                    'captchaOptions' => 
	              	array(
	              		'captcha' => 'ReCaptcha', 
	              		'service' => $recaptcha
	              	)
              ));
              
        //$this->addElement($captcha);
        
        $this->addElement(
        	'Button', 
        	'submitButton',
            array(
                'type'   	 => 'submit',
            	'required'   => false,
                'ignore'     => true,
                'label'      => 'Submit',
            	'iconClass'	 => 'myIcons okIcon',
                'style' 		=> 'margin-top: 10px;',  
            )
        );
    }
    
    public function isValid($data)
    {
		$password = $this->getElement ( 'password' );
		$confirmPassword = $this->getElement ( 'confirmPassword' );
		$password->addValidator ( new App_Validate_PasswordMatch ( $data ['confirmPassword'] ) );		
		return parent::isValid ( $data );
    }
}